package com.ywl.exercise.controller.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ywl.exercise.entity.GameEntity;
import com.ywl.exercise.entity.vo.ImageVoEntity;
import com.ywl.exercise.service.GameService;
import com.ywl.exercise.service.UserService;


@Controller
public class GameController {
	@Resource
	private UserService userServiceImpl;
	@Resource
	private GameService gameServiceImpl;
	
	@RequestMapping(value = "/startGame.do", method = RequestMethod.POST)
	public void startGame(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		//获取登录名
		String loginid = request.getParameter("loginid");
		//开始创建游戏
		GameEntity entity = new GameEntity();
		entity.setLoginid(loginid);
		entity.setScores(1000);
		
		Map<Integer, List<ImageVoEntity>> map = gameServiceImpl.startGame(entity);
		
		List<ImageVoEntity> imageVOs = null;
		int gameid = 0;
		for (Map.Entry<Integer, List<ImageVoEntity>> entry : map.entrySet()) {
			gameid = entry.getKey();
			imageVOs = entry.getValue();
		}
		
		entity.setGameid(gameid);
		
		HttpSession session = request.getSession();
		
		Random random = new Random();
		int index = random.nextInt(imageVOs.size());
		ImageVoEntity ive = imageVOs.get(index);
		
		//图片路径
		String picUrl = ive.getUrl().split(",")[0];
		//图片名字
		String picName = ive.getUrl().split(",")[1];
		int nameLen = picName.length();
		
		String[] str = ive.getAnswer().split(",");
		List<String> answers = new ArrayList<String>();
		for (int i = 0; i < str.length; i++) {
			answers.add(str[i]);
		}
		
		session.setAttribute("game", entity);
		session.setAttribute("picUrl", picUrl);
		session.setAttribute("picName", picName);
		session.setAttribute("nameLen", nameLen);
		session.setAttribute("answers", answers);
		session.setAttribute("imageVo", ive);
		
		//显示过的图片不再显示
		imageVOs.remove(index);
		session.setAttribute("imageVos", imageVOs);
	}
	
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/nextGame.do", method = RequestMethod.POST)
	public void nextGame(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		//获取成功过关后奖励的分数
		Integer score = Integer.valueOf(request.getParameter("score"));
		
		HttpSession session = request.getSession();
		List<ImageVoEntity> imageVOs = (List<ImageVoEntity>)session.getAttribute("imageVos");
		
		//将对象转化为json
		JSONObject jsonImage = new JSONObject();
		
		if(imageVOs.size() > 0) {
			ImageVoEntity ive = null;
			int index = 0;
			if(imageVOs.size() == 1) {
				ive = imageVOs.get(0);
			}else {
				Random random = new Random();
				index = random.nextInt(imageVOs.size());
				ive = imageVOs.get(index);
			}
			
			
			//更新数据库游戏的分数
			GameEntity game = (GameEntity)session.getAttribute("game");
			game.setScores(game.getScores()+score);
			gameServiceImpl.updateGame(game);
			
			//图片路径
			String picUrl = ive.getUrl().split(",")[0];
			//图片名字
			String picName = ive.getUrl().split(",")[1];
			int nameLen = picName.length();
			
			String[] str = ive.getAnswer().split(",");
			List<String> answers = new ArrayList<String>();
			for (int i = 0; i < str.length; i++) {
				answers.add(str[i]);
			}
			
			jsonImage.put("mark", ive.getMark());
			jsonImage.put("picUrl", picUrl);
			jsonImage.put("picName", picName);
			jsonImage.put("nameLen", nameLen);
			jsonImage.put("answers", answers);
			jsonImage.put("score",game.getScores());
			jsonImage.put("categoryname",ive.getCategoryname());
			
			session.setAttribute("game", game);
			session.setAttribute("imageVo", ive);
			//显示过的图片不再显示
			imageVOs.remove(index);
			session.setAttribute("imageVos", imageVOs);
		}else {
			jsonImage.put("gameover","gameover");
		}
		
		//json对象输出给客户端
		//设置编码格式
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.print(jsonImage.toString());
		out.flush();
		out.close();
	}
}
